using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Web.Framework.All
{
    public class TriStateCheckBox : WebControl, IPostBackDataHandler
    {        
        private int _InnerState = 3;                      

        private enum eStates
        {
            checkeds = 1,
            uncheck = 2,
            indeterminated = 3
        }

        public int CheckedNumber
        {
            get
            {
                return _InnerState;
            }
        }

        public bool? Checked
        {
            get
            {                               
                if (_InnerState == (int)eStates.indeterminated)
                {
                    return null;
                }
                else
                {
                    return _InnerState == (int)eStates.checkeds;
                }
            }
            set
            {
                if (!value.HasValue)
                {
                    _InnerState = (int)eStates.indeterminated;
                }
                else
                {
                    _InnerState = value.Value ? (int)eStates.checkeds : (int)eStates.uncheck;
                }
            }
        }       

        public string ImagePath { get; set; }                     
             
        protected override void Render(HtmlTextWriter output)
        {
            output.Write(string.Format("<span id='{0}' ImagePath='{1}' class='{2}' >", this.ClientID, this.ImagePath, this.CssClass));
            output.Write(string.Format("<img id='{0}' src='{1}{2}.bmp' alt=''  alt='' />", this.ClientID, this.ImagePath, this._InnerState));
            output.Write(string.Format("<input type='hidden' value='{1}' id='{0}' name='{0}' />", "hidthree_" + this.UniqueID, this._InnerState));
            output.Write("</span>");           
        }             

        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                _InnerState = (int)savedState;
            }
        }

        protected override object SaveViewState()
        {
            return _InnerState;
        }      
       
        protected virtual void OnChange(EventArgs args)
        {
            if (Change != null)
            {
                Change(this, args);
            }
        }

        public event EventHandler Change;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Page != null)
            {
                Page.RegisterRequiresPostBack(this);
            }
        }

        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            bool loadPost = false;
            string formValue = HttpContext.Current.Request.Form["hidthree_" + this.UniqueID];
            if (!String.IsNullOrEmpty(formValue))
            {
                int state = Convert.ToInt32(formValue);
                loadPost = state != _InnerState;
                _InnerState = state;
            }

            return loadPost;
        }

        public void RaisePostDataChangedEvent()
        {
            OnChange(new EventArgs());
        }
    }
}
